﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ParticleEditor
{
    public partial class Form1 : Form
    {
        SGP.ManagedDirect3D d3D;//SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager tm;// SGP.ManagedTextureManager.Instance;

        bool looping = true;
        Emitter LiveEmitter = new Emitter();
        Color startColor = Color.DarkRed;
        Color endColor = Color.DarkGray;
        int ImageTag;
        //double CurrTime = DateTime.Now.Ticks;
        DateTime CurrTime = System.DateTime.Now;
        RandomToolForm tf = null;

        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public Form1()
        {
            InitializeComponent();

        }

        public void Init()
        {
            d3D = SGP.ManagedDirect3D.Instance;
            tm = SGP.ManagedTextureManager.Instance;

            d3D.InitManagedDirect3D(pictureBox1);
            tm.InitManagedTextureManager(d3D.Device, d3D.Sprite);

            Looping = true;
            ImageTag = tm.LoadTexture("../../Resources/Flash.png", 0);
            LiveEmitter.SpawnParticles(400, 4, 8, 50, 90, 50, 90, 4, 4, 4, 4);

           

        }
        public void Render()
        {
            d3D.Clear(pictureBox1, Color.Black);
            Rectangle rect = new Rectangle();
            rect.X = 20;
            rect.Y = 20;
            rect.Width = 50;
            rect.Height = 50;

            d3D.DeviceBegin();
            d3D.SpriteBegin();

            //draw zone
            d3D.DrawRect(rect, endColor);

            foreach (Particle p in LiveEmitter.LiveParticles)
            {
                if (p.IsDead != true)
                {
                    //int tag = tm.LoadTexture("C:/Development/C++ Projects/TriQuestRepository/ParticleEditor/Flash.png", 0);
                    int centerX = tm.GetTextureWidth(ImageTag) / 2;
                    int centerY = tm.GetTextureHeight(ImageTag) / 2;
                    tm.Draw(ImageTag, (int)p.PositionX, (int)p.PositionY, 1.0f, 1.0f, Rectangle.Empty, (int)p.PositionX + centerX, (int)p.PositionY + centerY, 0, startColor);
                    // e.Graphics.DrawImage(LiveEmitter.ParticleImage, new Point((int)p.PositionX, (int)p.PositionY)/*new Point(50, 50)*/);
                }
            }

            //End Draw zone
            d3D.SpriteEnd();
            d3D.DeviceEnd();

            d3D.Present();

        }

        // float CurrTime = (float)DateTime.Now.Ticks


        public void UpdateFrame()
        {

            //double nowTime = DateTime.Now.Ticks;

            //double fElapsedTime = (nowTime - CurrTime) / 10000000.0f;

            //CurrTime = nowTime;

            //if (fElapsedTime >= 0.0125f)
            //    fElapsedTime = 0.0125f;

            //LiveEmitter.Update(fElapsedTime);
            DateTime nowTime = System.DateTime.Now;

           TimeSpan fElapsedTime = (nowTime - CurrTime);

           CurrTime = nowTime;

           double elapTime = fElapsedTime.TotalSeconds;

           if (elapTime >= 0.0125f)
                  elapTime = 0.0125f;

           LiveEmitter.Update(elapTime);
        }












        private void graphicsPanel1_Paint(object sender, PaintEventArgs e)
        {
        }
        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

            LiveEmitter.ResetAllParticles();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (tf == null)
            {
                tf = new RandomToolForm();
                tf.FormClosed += new FormClosedEventHandler(tool_FormClosed);
                button4.Enabled = false;
                tf.Show();
            }
        }

        private void tool_FormClosed(object sender, FormClosedEventArgs e)
        {
            tf = null;
            button4.Enabled = true;
           // throw new NotImplementedException();
        }

        private void Form1_MouseHover(object sender, EventArgs e)
        {

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void graphicsPanel1_MouseMove(object sender, MouseEventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Looping = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ColorDialog DLG = new ColorDialog();
            DLG.ShowDialog();
            startColor = DLG.Color;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ColorDialog DLG = new ColorDialog();
            DLG.ShowDialog();
            endColor = DLG.Color;
        }

        private void imageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                ImageTag = tm.LoadTexture(dlg.FileName, 0);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

    }
}
